package edu.ccut.saturn.studio.database.model;

import org.eclipse.swt.graphics.Image;


import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;

/**
 * 表中一列
 * 
 * @author 谷钰
 * @version 1.0 2008.12.02
 * 
 */
public class SqlColumn extends SaturnTree {

	private static final String VIEWTOOLTIP = Messages.getString("SqlColumn.viewname"); //$NON-NLS-1$
	private static final String TABLETOOLTIP = Messages
			.getString("SqlColumn.tablename"); //$NON-NLS-1$

	private String tableName;
	private String columnName;
	private String columnDataType = ""; //$NON-NLS-1$
	private String isNull;
	private int primaryKeyOrder;
	private int columnLenth;

	public SqlColumn(String s) {
		super(s);
	}

	@Override
	public Image getImage() {
		if (this.isKey()) {
			return Activator.getImage(Images.DATABASE_COLUMN_PRIMER_KEY);
		} else {
			return Activator.getImage(Images.DATABASE_COLUMN);
		}
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public String getColumnName() {
		return columnName;
	}

	public void setColumnName(String columnName) {
		this.columnName = columnName;
	}

	public String getColumnDataType() {
		return columnDataType;
	}

	public void setColumnDataType(String columnDataType) {
		this.columnDataType = columnDataType;
	}

	public String getIsNull() {
		return isNull;
	}

	public void setIsNull(String isNull) {
		this.isNull = isNull;
	}

	public boolean isKey() {
		return getPrimaryKeyOrder() > 0;
	}

	public int getColumnLenth() {
		return columnLenth;
	}

	public void setColumnLenth(int columnLenth) {
		this.columnLenth = columnLenth;
	}

	public int getPrimaryKeyOrder() {
		return primaryKeyOrder;
	}

	public void setPrimaryKeyOrder(int primaryKeyOrder) {
		this.primaryKeyOrder = primaryKeyOrder;
	}

	public String toString() {
		return getName() + " : " + getColumnDataType() + "(" //$NON-NLS-1$ //$NON-NLS-2$
				+ getColumnLenth() + ")"; //$NON-NLS-1$
	}

	public String getDescription() {

		String tableOrView = null;
		if (super.getParent() instanceof SqlTable) {
			tableOrView = TABLETOOLTIP;
		} else {
			tableOrView = VIEWTOOLTIP;
		}

		String s1 = getParent().getName();
		StringBuffer stringbuffer = new StringBuffer();
		stringbuffer.append("<b>").append(tableOrView).append("<b>"); //$NON-NLS-1$ //$NON-NLS-2$
		stringbuffer.append("<p>").append(s1); //$NON-NLS-1$

		return stringbuffer.toString();
	}  

}
